package com.mybatis.mp.scenery.config;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

@Configuration
public class RabbitMqConfig {

    @Bean
    public Queue testQueue(){
        Queue queue = new Queue("baiye",true);
        return queue;
    }

    @Bean
    public Exchange testExchange(){
        Exchange exchange = new DirectExchange("baiye",true,false);
        return exchange;
    }

    @Bean
    public Binding bindingBaiye(){
        Binding binding = BindingBuilder.bind(testQueue()).to(testExchange()).with("baiye").noargs();
        return binding;
    }

    @RabbitListener(queues = "baiye")
    public void custome(String data, Channel channel, Message message) throws IOException {




        try {

            System.out.println(data);

            // 消费完成
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

        } catch (Exception e) {


            // 转到死信队列处理
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);


        }
    }

}
